/**
 * Copyright (C) 2011 Angelo Zerr <angelo.zerr@gmail.com> and Pascal Leclercq <pascal.leclercq@gmail.com>
 * 
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package fr.opensagres.xdocreport.core.utils;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.logging.Logger;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;

import fr.opensagres.xdocreport.core.logging.LogUtils;

public class XMLUtils
{

    private static final Integer INDENT_NUMBER = new Integer(4);
    /**
     * Logger for this class
     */
    private static final Logger logger = LogUtils.getLogger(XMLUtils.class);

    public static String prettyPrint(String in) {

        return prettyPrint(in, INDENT_NUMBER);
    }

    public static String prettyPrint(String in, int indent) throws TransformerFactoryConfigurationError {
        try {
            TransformerFactory transfac = TransformerFactory.newInstance();

            transfac.setAttribute("indent-number", indent);
            Transformer trans = transfac.newTransformer();

            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            // create string from xml tree
            final StringWriter out = new StringWriter();

            trans.transform(new StreamSource(new StringReader(in)), new StreamResult(out));
            return out.toString();
        } catch (TransformerException e) {
            logger.severe("Unable to pretty print : " + e); //$NON-NLS-1$

        }

        return in;
    }

    /**
     * Get the SAX {@link AttributesImpl} of teh given attributes to modify
     * attribute values.
     * 
     * @param attributes
     * @return
     */
    public static AttributesImpl toAttributesImpl(Attributes attributes) {
        if (attributes instanceof AttributesImpl) {
            return (AttributesImpl) attributes;
        }
        // Another SAX Implementation, create a new instance.
        AttributesImpl attributesImpl = new AttributesImpl();
        int length = attributes.getLength();
        for (int i = 0; i < length; i++) {
            attributesImpl.addAttribute(attributes.getURI(i), attributes.getLocalName(i), attributes.getQName(i),
                    attributes.getType(i), attributes.getValue(i));
        }
        return attributesImpl;
    }
}
